// Author: wsfuyibing <682805@qq.com>
// Date: 2025-06-23

package order

import (
	"context"
	"gitee.com/go-libs/db-xorm/db"
	"server/app/errs"
	"server/app/http/models"
	"server/app/http/services"
	"server/app/kind"
)

type Logic struct {
}

func (o *Logic) GetLogs(_ context.Context, sess *db.Session, id int64) (list []*models.OrderLog, err error) {
	if err = sess.Where("order_id = ?", id).OrderBy("created_at ASC").Find(&list); err != nil {
		err = errs.ErrDatabaseQuery
	}
	return
}

func (o *Logic) GetOrderByNo(ctx context.Context, sess *db.Session, ai kind.ArcadeId, on kind.OrderNo) (model *models.Order, err error) {
	var has bool

	// 1. 读取主记录.
	if model, has, err = services.NewOrderService(sess).Dao.GetByMap(ctx, map[string]any{
		// "arcade_id": ai,
		"order_no": on,
	}); err != nil {
		err = errs.ErrDatabaseQuery
		return
	}

	if !has {
		err = errs.ErrOrderNotFound
		return
	}

	return
}

func (o *Logic) GetPayKinds(ctx context.Context, sess *db.Session, id int64) (list []*models.OrderPayKind, err error) {
	if list, err = services.NewOrderPayKindService(sess).Dao.ListByMap(ctx, map[string]any{
		"order_id": id,
	}); err != nil {
		err = errs.ErrDatabaseQuery
	}
	return
}
